<?php 
	class Videos extends CI_Model{

	 	function __construct(){
        	parent::__construct();
    	}

    	/**
        * 函数功能介绍：返回视频列表（用于分页显示）
        * 创建人:  Yang Ningshu
        * 创建时间: 2014-08-21 11:08
        * 最后修改人：Zhang ShaoWei
        * 最后修改时间：2014-08-29 15:08
        * @param id int
        * @return array
        */
        function get_list_ofpage($limit = 0, $offset = 10,$data = 0){
            $school_id = (int)$this->user_data['school_id'];

            $where = "1=1 and v.school_id = ".$school_id;
            $order = "v.id desc";
            if(!empty($data['sort'])){
                 if($data['sort'] == 1 and $data['asc_des'] == 2){
                    $order = "v.baidu_recommended asc";
                }else if($data['sort'] == 1 and $data['asc_des'] == 1){
                    $order = "v.baidu_recommended desc";
                }else if($data['sort'] == 2 and $data['asc_des'] == 2){
                    $order = "v.click_num asc";
                }else if($data['sort'] == 2 and $data['asc_des'] == 1){
                    $order = "v.click_num desc";
                }else if($data['sort'] == 3 and $data['asc_des'] == 2){
                    $order = "v.wap_recommended asc";
                }else if($data['sort'] == 3 and $data['asc_des'] == 1){
                    $order = "v.wap_recommended desc";
                }else if($data['sort'] == 4 and $data['asc_des'] == 2){
                    $order = "v.add_time asc";
                }else if($data['sort'] == 4 and $data['asc_des'] == 1){
                    $order = "v.add_time desc";
                }else if($data['sort'] == 5 and $data['asc_des'] == 2){
                    $order = "v.status asc";
                }else if($data['sort'] == 5 and $data['asc_des'] == 1){
                    $order = "v.status desc";
                }else if($data['sort'] == 6 and $data['asc_des'] == 2){
                    $order = "v.sort asc";
                }else if($data['sort'] == 6 and $data['asc_des'] == 1){
                    $order = "v.sort desc";
                }
            }
            if(!empty($data['aid'])){
                $sql = "SELECT video_id FROM {$this->db->dbprefix}activity  WHERE id=".$data['aid']."";
                $result = $this->db->query($sql)->result_array();
                if($result[0]['video_id']){
                    $where.=" and v.id in (".$result[0]['video_id'].")";
                }else{
                    $result[0]['video_id'] = 0;
                    $where.=" and v.id in (".$result[0]['video_id'].")";
                }
            }

            if(!empty($data['review'])){
                $where .= " AND v.review = '". $data['review']."'";
            }
            
            if(!empty($data['cate_id'])){
                $where .= " AND v.cate_id = '". $data['cate_id']."'";
            }

            if(!empty($data['subject_id'])){
                $where .= " AND v.subject_id = '". $data['subject_id']."'";
            }
            
            if(!empty($data['search'])) {
                $where .= " AND v.title like '%".$data['search']."%'";
            }

            $sql = "SELECT v.id,v.cate_id,v.uid,v.video_size,v.image_url,v.review,v.duration,v.title,v.click_num,v.add_time,v.sort,v.status, u.nick FROM {$this->db->dbprefix}video as v left join {$this->db->dbprefix}user as u on u.id = v.uid WHERE $where ORDER BY ".$order." LIMIT $limit, $offset";
            $result = $this->db->query($sql)->result_array();
            $sql = "SELECT count(v.id) AS count FROM {$this->db->dbprefix}video as v left join {$this->db->dbprefix}user as u on u.id = v.uid WHERE $where";
            $count = $this->db->query($sql)->result();
            return array("count" => $count[0]->count, "result" => $result);
        }



        function get_video_info($id){
            if(empty($id)) return array();
            $sql = "SELECT v.id,v.subject_id,v.cate_id,v.description,v.course_id,v.title,v.click_num,v.real_click_num,v.base_click,v.add_time,v.edit_time,v.image_url,v.video_url,v.duration,v.sort,v.status,v.comment_count,u.nick FROM {$this->db->dbprefix}video as v left join {$this->db->dbprefix}user as u on u.id = v.uid ";
            $sql.= " WHERE ";
            if(is_array($id)) {
                $ids = implode(",",$id);
                $sql.= " v.id IN (". $ids .") ";
            }else {
                $sql.= " v.id=".$id."";
            }
            
            $result = $this->db->query($sql)->result_array();
            return $result;
        }
    	
	}